package de.upb.messerschmidt.set;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.upb.messerschmidt.set.entity.Attendee;
import de.upb.messerschmidt.set.entity.Event;
import de.upb.messerschmidt.set.entity.EventStatus;
import de.upb.messerschmidt.set.entity.Person;
import de.upb.messerschmidt.set.jdo.PMF;
import de.upb.messerschmidt.set.util.Utils;

/**
 * Will be used if a {@link Person} is already attending to an {@link Event} and
 * wants to change his status
 * 
 * @author Tim Messerschmidt
 * 
 */
public class RefreshAttendeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = req.getParameter("person");
		String status = req.getParameter("status");
		String event = req.getParameter("event");

		if (username != null && status != null) {
			checkAndRefreshStatus(resp, username, status, event);
		}
	}

	@SuppressWarnings("unchecked")
	private void checkAndRefreshStatus(HttpServletResponse resp,
			String username, String status, String event) throws IOException {
		Person person = null;
		Attendee attendee = null;

		String[] name = Utils.nameParser(username);

		PersistenceManager pm = PMF.get().getPersistenceManager();

		// Query for the User
		Query personQuery = pm.newQuery(Person.class);
		personQuery.setFilter("name == nameParam && prename == prenameParam");
		personQuery.declareParameters("String nameParam, String prenameParam");

		List<Person> persons = (List<Person>) personQuery.execute(name[0],
				name[1]);
		List<EventStatus> eventStatusList = null;
		List<Attendee> attendeeList = null;

		if (!persons.isEmpty()) {
			person = persons.get(0);
			eventStatusList = person.getEvents();
		}

		if (person != null && eventStatusList != null) {
			for (EventStatus es : eventStatusList) {
				if (es.getEvent().equals(event)) {
					if (es.getStatus().equals(status)) {
						resp.getWriter().println("Status already set");
						resp.setStatus(200);
					} else {
						Query attendeeQuery = pm.newQuery(Attendee.class);
						attendeeQuery
								.setFilter("event == eventParam && person == personParam");
						attendeeQuery
								.declareParameters("String eventParam, String personParam");

						attendeeList = (List<Attendee>) attendeeQuery.execute(
								es.getEvent(), name[1] + " " + name[0]);

						if (!attendeeList.isEmpty()) {
							attendee = attendeeList.get(0);
						}

						if (attendee != null) {
							es.setStatus(status);
							attendee.setEventStatus(status);
							resp.setStatus(200);
							resp.getWriter().println(
									"Status successfully refreshed");
							pm.flush();
						} else {
							resp.getWriter().println(
									"Can't find Attendee: " + name[1] + " "
											+ name[0] + " at: " + event);
							resp.setStatus(200);
						}
					}
				}
			}
		} else {
			resp.setStatus(400);
			resp.getWriter().println("Status update failed");
		}
		pm.close();
	}
}
